package javaWeb;

import Bean.User;
import Dao.BasicDao;
import Dao.Impl.UserDaoImpl;
import Service.Impl.MemberServiceImpl;
import Service.MemberService;
import Utils.DBHelp;
import com.google.gson.Gson;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;

import static com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY;

public class MemberServlet extends BasicServlet {
    MemberService memberService = new MemberServiceImpl();

    protected void Register(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String email = req.getParameter("email");
        BasicDao<User> userBasicDao = new BasicDao<>();
        String sql = "select * from user where name = ?;";
        User user = null;
        HttpSession session = req.getSession();
        String captcha = req.getParameter("captcha");
        String code = (String) session.getAttribute(KAPTCHA_SESSION_KEY);
        try {
            user = userBasicDao.selectOne(sql, User.class, username);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
        }
        if (code != null && code.equals(captcha)) {
            if (user == null) {
                String one = "INSERT INTO user (name, passWord, email) VALUES (?, MD5(?), ?);";
                userBasicDao.dml(one, username, password, email);
                resp.sendRedirect("/Web/views/member/register_ok.html");
            } else {
                req.setAttribute("msg", "用户或密码错误");
                req.setAttribute("username", username);
                req.getRequestDispatcher("/views/member/login.jsp").forward(req, resp);
            }
        } else {
            req.setAttribute("msg", "验证码错误");
            req.setAttribute("username", username);
            req.setAttribute("active", "Register");
            req.getRequestDispatcher("/views/member/login.jsp").forward(req, resp);
        }

    }

    private void Login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/json:charset=utf-8");

        String username = req.getParameter("user-name");
        String password = req.getParameter("user-password");
        HttpSession session = req.getSession();

        session.removeAttribute(KAPTCHA_SESSION_KEY);
        if (memberService.LoginUser(username, password)) {
            req.setAttribute("isLogin", true);
            User user = memberService.getUser(username);
            session = req.getSession();
            session.setAttribute("User", user);


            resp.sendRedirect("/Web/views/member/login_ok.jsp");
        } else {
            req.setAttribute("msg", "用户或密码错误");
            req.setAttribute("username", username);
            req.getRequestDispatcher("/views/member/login.jsp").forward(req, resp);
        }
    }


    protected void exit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        session.invalidate();
        resp.sendRedirect("/Web/javaWeb/CustomerFurnPage?action=FurnPage");
    }
    protected void isUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        boolean existsUsername = memberService.isExistsUsername(username);
        Gson gson = new Gson();
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("isExist",existsUsername);
        String json = gson.toJson(hashMap);
        resp.getWriter().write(json);
    }
    protected void isCode(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String codeNumber = req.getParameter("codeNumber");
        HttpSession session = req.getSession();
        String code = (String) session.getAttribute(KAPTCHA_SESSION_KEY);
        boolean existsUsername = false;
        if (codeNumber != null ||  code.equals(codeNumber)){
            existsUsername = true;
        }
        Gson gson = new Gson();
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("isExit",existsUsername);
        String json = gson.toJson(hashMap);
        resp.getWriter().write(json);
    }


}
